package com.tianyuchan.exception.contract.impl;

import com.tianyuchan.exception.contract.ContractManage;
import com.tianyuchan.exception.enums.ContractManageErrorCode;
import com.tianyuchan.exception.exception.ContractManageAssert;
import com.tianyuchan.exception.dto.req.ContractManageReq;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @Classname : ContractManageImpl
 * @Description : 合同管理服务实现类
 * @Author : cty
 * @Date 2023/3/19 22:55
 */

@Log4j
@Service
public class ContractManageImpl implements ContractManage {

    @Override
    public String createContract(ContractManageReq contractManageReq) {
        // 合同非空检查
        Objects.requireNonNull(contractManageReq.getContractGuid());
        Objects.requireNonNull(contractManageReq.getContractCode());
        Objects.requireNonNull(contractManageReq.getContractName());
        Objects.requireNonNull(contractManageReq.getContractAmount());
        BigDecimal contractAmount = contractManageReq.getContractAmount();

        // 合同金额非负检查
        ContractManageAssert.isTrue(
                contractAmount.compareTo(BigDecimal.ZERO) >= 0,
                ContractManageErrorCode.CONTRACT_AMOUNT_IS_NEGATIVE,
                contractAmount);

        String res = String.format("合同[%s]创建成功！", contractManageReq.getContractGuid());
        log.info(res);
        return res;
    }

    @Override
    public String payContract(ContractManageReq contractManageReq) {
        // 合同非空检查
        Objects.requireNonNull(contractManageReq.getContractGuid());
        Objects.requireNonNull(contractManageReq.getContractCode());
        Objects.requireNonNull(contractManageReq.getContractName());
        Objects.requireNonNull(contractManageReq.getContractAmount());
        Objects.requireNonNull(contractManageReq.getPaymentAmount());
        BigDecimal contractAmount = contractManageReq.getContractAmount();
        BigDecimal paymentAmount = contractManageReq.getPaymentAmount();

        // 支付金额检查
        ContractManageAssert.isLessThanOrEqualTo(
                paymentAmount,
                contractAmount,
                ContractManageErrorCode.PAY_AMOUNT_IS_GREATER_THAN_CONTRACT_AMOUNT);

        String res = String.format("合同[%s]成功支付[%s]元！", contractManageReq.getContractGuid(), paymentAmount);
        log.info(res);
        return res;
    }
}
